package top.infopub.mgr.hr.web;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import top.infopub.generic.GenericController;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.ErrorCodeEnum;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.hr.service.PostInfoService;
import top.infopub.mgr.hr.service.StaffInfoService;
import top.infopub.mgr.sys.service.OperatorService;
import top.infopub.mgr.util.LogUtil;
import top.infopub.mgr.util.LogUtil.AcctionType;
import top.infopub.mgr.util.LogUtil.LogModelType;
import top.infopub.model.hr.PostInfo;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.github.pagehelper.Page;
import top.infopub.model.hr.StaffInfo;
import top.infopub.model.sys.Operator;
import top.infopub.security.util.SessionUserUtils;

import javax.servlet.http.HttpServletRequest;


/**
 * 岗位管理
 * @author wangw
 * @version 2020年1月8日
 * @see PostInfoController
 * @since
 */
@Controller
@RequestMapping("/post")
public class PostInfoController extends GenericController {

    @Autowired
    private PostInfoService postInfoService;
    @Autowired
    private StaffInfoService staffInfoService;
    @Autowired
    private OperatorService operatorService;

    /**
     * 部门列表数据
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<PostInfo> queryDataByCondition(PostInfo postInfo, ParamPage paramPage) {
        Page<PostInfo> page = new Page<PostInfo>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        page = postInfoService.selectPageByDomainWhere(page, postInfo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    @RequestMapping("/queryDataByGroupId")
    @ResponseBody
    public RspVo queryDataByGroupId(String groupId,String roleType, ParamPage paramPage) {
        if("1".equals(roleType)){
            Page<PostInfo> page = new Page<PostInfo>();
            page.setPageNum(paramPage.getPage());
            page.setPageSize(paramPage.getLimit());
            page = postInfoService.selectPageByGroupId(page, groupId,roleType);
            return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
        }else{
            Page<StaffInfo> page = new Page<StaffInfo>();
            page.setPageNum(paramPage.getPage());
            page.setPageSize(paramPage.getLimit());
            page = staffInfoService.selectPageByGroupId(page, groupId,roleType);
            return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
        }

    }


    /**
     * 新增/编辑岗位
     * @param postInfo
     * @return
     * @see
     */
    @SuppressWarnings("unchecked")
    @RequestMapping("/save")
    public @ResponseBody RspVo<PostInfo> save(PostInfo postInfo){
        if (StringUtils.isEmpty(postInfo.getPostName())
            || StringUtils.isEmpty(postInfo.getDeptId())
            || StringUtils.isEmpty(postInfo.getOrgId())){
            return ErrorCodeEnum.ERROR_COMMON_PARAM.getRspVo("缺少参数");
        }
        if(StringUtils.isEmpty(postInfo.getId())){
            PostInfo infoWhere = new PostInfo();
            infoWhere.setDeptId(postInfo.getDeptId());
            infoWhere.setPostName(postInfo.getPostName());
            infoWhere.setState(1);
            List<PostInfo> postInfos = postInfoService.selectByEntityWhere(infoWhere);
            if(CollectionUtils.isNotEmpty(postInfos)){
                return ErrorCodeEnum.ERROR_COMMON_SAVE.getRspVo("岗位名称【"+postInfo.getPostName()+"】已经存在~");
            }
        }else{
            List<PostInfo> postInfos = postInfoService.selectByPostNameAndId(postInfo.getPostName(),postInfo.getDeptId(),postInfo.getId());
            if(CollectionUtils.isNotEmpty(postInfos)){
                return ErrorCodeEnum.ERROR_COMMON_SAVE.getRspVo("岗位名称【"+postInfo.getPostName()+"】已经存在~");
            }
        }
        postInfoService.save(postInfo);
        if(StringUtils.isNotEmpty(postInfo.getId())){
            LogUtil.bizLog(LogModelType.OTHER, AcctionType.ADD, "新增部门 - 部门名称:" + postInfo.getPostName().trim(), null);
        }else{
            LogUtil.bizLog(LogModelType.OTHER, AcctionType.UPDATE, "修改部门 - 部门Id:" + postInfo.getId(), null);
        }
        return RspVo.getSuccessResponse(postInfo);
    }

    @RequestMapping("/list")
    public String list(HttpServletRequest request) {
        String bindOrgId = (String) SessionUserUtils.getSessionAttribute("bindOrgId");
        //request.setAttribute("bindOrgId", bindOrgId);
       // String visibleOrgIds = (String)SessionUserUtils.getSessionAttribute("visibleOrgIds");
        /*List<OrgInfo> orgInfos = new ArrayList<OrgInfo>();
        if(StringUtils.isEmpty(visibleOrgIds)){
            Map<String, Object> where = Maps.newHashMap();
            where.put("state", 1);
            where.put("orderCondition", " types asc, add_time asc");
            orgInfos = orgInfoService.selectByMapWhere(where);
        }else{
            orgInfos = orgInfoService.selectByVisibleIds(visibleOrgIds);
        }
        request.setAttribute("orgInfos", orgInfos);*/
        return "hr/lay-postInfo-list1";
    }

    @RequestMapping("/getPostByName")
    @ResponseBody
    public Map<String, Object> getPostByName(String postName,String type,String roleType) {
        boolean flag = true;
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            postName="%"+postName+"%";
            List list =new ArrayList();
            if("1".equals(roleType)){//岗位
                if("4".equals(type)){
                    type="";
                }
                list = postInfoService.selectPostByName(postName,type);
            }else{//人员
               // list = postInfoService.selectPostByName(postName,type);
                Operator operator=new Operator();
                operator.setSrealName(postName);
                list = operatorService.selectByEntityWhere(operator);
            }

            map.put("postInfos", list);
        }
        catch (Exception e) {
            log.error("根据名称查找岗位异常", e);
            flag = false;
        }
        map.put("flag", flag);
        return map;
    }
}
